<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table 
          border 
          :data="list" 
         row-key="id">
      <el-table-column label="名称" prop="name" />
      <el-table-column label="标识" prop="code" />
      <el-table-column label="描述" prop="description" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.type === 1" @click="hAdd(2,scope.row.id)">添加</el-button>
          <el-button type="text" @click="editPermiss">编辑</el-button>
          <el-button type="text" @click="hDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
      </el-card>
      <!-- 弹框 -->
      <el-dialog :close-on-click-modal="false" :title="isEdit ? '编辑' : '添加'" :visible.sync="showDialog">
         <!-- 表单内容 -->
  <el-form label-width="100px">
    <el-form-item label="权限名称">
      <el-input v-model="formData.name" />
    </el-form-item>
    <el-form-item label="权限标识">
      <el-input v-model="formData.code" />
    </el-form-item>
    <el-form-item label="权限描述">
      <el-input v-model="formData.description" />
    </el-form-item>
    <el-form-item label="权限启用">
        <!-- 开关 -->
      <el-switch
        v-model="formData.enVisible"
        active-text="启用"
        active-value="1"
        inactive-text="不启用"
        inactive-value="0"
      />
    </el-form-item>
    <div style="text-align: right;">
      <el-button @click="showDialog = false">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </div>
  </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPermissionList,addPermission,delPermission, getPermissionDetail } from '@/api/permissions'
// 数组转树
import { array2Tree } from '@/utils/index'
export default {
 data() {
  return {
    list: [],
    showDialogRole: false, // 角色弹框
    showDialog: false, // 是否显示弹层
    formData: {
      name: '', // 名称
      code: '', // 权限标识
      description: '', // 描述
      enVisible: '0', // 开启
      pid: '', // 添加到哪个节点下
      type: '' // 类型
    },
    isEdit: false // 是否为编辑
  }
},
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('获取权限数据', res)
        this.list = array2Tree(res.data, '0')
      } catch (err) {
        console.log(err)
      }
    },
    hAdd(type,id){
      this.formData.name = this.formData.code = this.formData.description = ''
      this.showDialog = true
      this.isEdit = false
      this.formData.type = type
      this.formData.pid = id
    },
    // --------------------------弹框确认按钮
    hSubmit(){
      // 正则校验
      this.doAdd()
    },
    async doAdd(){
      // 调用接口
      await addPermission(this.formData)
      // 关闭弹层
      this.showDialog = false
      // 刷新列表
      this.loadPermissionList()
    },
    // --------------------点击删除显示提示弹框
    hDel(id) {
      // 确认框
      this.$confirm('是否要退出', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    // 调用接口做删除业务
    async doDel(id){
      // 1.调用接口做删除
      await delPermission(id)
      this.$message.success('删除成功')
      // 2.重发请求
      this.loadPermissionList()
    },
    // --------------------编辑弹框，数据回填
    editPermiss(id){
      this.showDialog = true
      // 编辑
      this.isEdit = true
      // 数据回填
      // 发送请求获取数据
      // const res = await getPermissionDetail(id)
      // console.log('选中该列的信息',res)
      // this.formData = res.data
    },
  }
}
</script>